package com.ys.controller.api.sys;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.dto.DtreeSqlDto;
import com.ys.dto.YsTreeDto;
import com.ys.entity.sys.TsOrganizationInfo;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.TsOrganizationInfoService;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tsOrganizationInfo")
public class ApiInterTsOrganizationInfoController {
	
	@Autowired
	private CommonApiService<TsOrganizationInfo> commonApiService;
	@Autowired
	private TsOrganizationInfoService tsOrganizationInfoService;

	@RequiresPermissions("tsOrganizationInfo:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(
			@ModelAttribute TsOrganizationInfo cond,
			Long nodeId
			){
		cond.where("A.OrgKind != 'user'");
		cond.addLeftJoin("TS_OrganizationInfo B on B.id = A.pid")
			.addField("B.Orgid porgid")
			.addField("B.orgName porgName");
		cond.addLeftJoin("TS_UserInfo C on C.UserId = A.HeadUserId")
			.addField("C.UserName headUserName");
		
		if (nodeId != null) {
			TsOrganizationInfo entity = new TsOrganizationInfo().setId(nodeId).queryFirst();
			cond.where("A.NodeLft >= " + entity.getNodeLft())
				.where("A.NodeRgt <= " + entity.getNodeRgt());
		}
		
		return commonApiService.searchLay(cond);
	}

	@RequestMapping("/searchForModal")
    @ResponseBody
	public LayuiTableVoJA searchForModal(Model model,
			@ModelAttribute TsOrganizationInfo cond){
		cond.where("A.OrgKind != 'user'");
		cond.addField("A.headUserId + '/' + U.UserName AS headUserName");
		cond.addLeftJoin("TS_UserInfo U ON U.UserId = A.headUserId AND 1=1");
		cond.setStatus(1);
		return commonApiService.searchLay(cond);
	}

	@RequestMapping("/searchForModalByDept")
	@ResponseBody
	public LayuiTableVoJA searchForModalByDept(Model model,
			@ModelAttribute TsOrganizationInfo cond){

		cond.addField("A.headUserId + '/' + U.UserName AS headUserName");
		cond.addLeftJoin("TS_UserInfo U ON U.UserId = A.headUserId AND A.headUserId IS NOT NULL");

		cond.where("A.OrgKind = 'dept' ");
		cond.setStatus(1);
		LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);

		return tableVoJA;
	}


	@RequestMapping("/searchForModalByCompany")
	@ResponseBody
	public LayuiTableVoJA searchForModalByCompany(Model model,
			@ModelAttribute TsOrganizationInfo cond){
		cond.where("A.OrgKind = 'company' ");
		LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);
		return tableVoJA;
	}



	//组织机构弹出框，带部门底下的用户信息
	@RequestMapping("/searchForModalWithUser")
	@ResponseBody
	public LayuiTableVoJA searchForModalWithUser(
			@ModelAttribute TsOrganizationInfo cond,
			String conditions){
		cond.addLeftJoin("TS_UserInfo U ON U.UserId = A.HeadUserId");
		cond.addField("U.UserId + '/' + U.UserName AS _ys_nameLinked");
		cond.addField("U.RoleName AS roleName");
		cond.addField("U.UserId AS userId");
		cond.addField("U.UserName AS userName");
		cond.where("A.OrgKind = 'dept' AND U.Status = 1 ");
		cond.setOrderby("U.UserId ASC");
		String nameLinked = ConditionUtil.getConditionValue(conditions, "_ys_nameLinked");//工号/姓名
		if (nameLinked != null){
			cond.where("U.UserId LIKE '%" + nameLinked + "%'" + " OR  U.UserName LIKE '%" + nameLinked + "%'");
		}
		cond.setStatus(1);
		LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);
		return tableVoJA;
	}


	//组织机构弹出框，财务工资小计部门维护表使用
	@RequestMapping("/searchForModalByTcwFinanceSalaryClassify")
	@ResponseBody
	public LayuiTableVoJA searchForModalByTcwFinanceSalaryClassify(@ModelAttribute TsOrganizationInfo cond){
		cond.where("A.OrgKind = 'dept' OR A.Orgid = 'C101'");
		cond.addField("CASE " +
				"WHEN A.Orgid = 'D250' THEN '研发部' " +
				"WHEN A.Orgid = 'C101' THEN '总裁办' " +
				" ELSE A.OrgName END orgNameNew");//部门名称

		cond.where("( SELECT COUNT ( 1 ) AS num FROM TCW_FinanceSalaryClassify B WHERE B.Orgid = A.Orgid ) = 0 ");
		cond.setStatus(1);
		LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);
		return tableVoJA;
	}

	@RequestMapping("/dtree")
    @ResponseBody
	public DtreeSqlDto dtree(){
		return tsOrganizationInfoService.dtree();
	}

	@RequestMapping("/ysTree")
    @ResponseBody
	public YsTreeDto ysTree(){
		return tsOrganizationInfoService.ysTree();
	}
	
	/**
	 * 新增
	 */
	@PostMapping("/add")
    @ResponseBody
	public R add(Long pid,
			String orgName,
			String orgKind){
		
		return tsOrganizationInfoService.add(pid, orgName, orgKind);
	}
	
	/**
	 * 编辑
	 */
	@RequiresPermissions("tsOrganizationInfo:edit")
	@PostMapping("/edit")
    @ResponseBody
	public R edit(Long id,
			String orgName,
			String headUserId, int status){
		
		return tsOrganizationInfoService.edit(id, orgName, headUserId,status);
	}
	@RequestMapping("/searchZZJG")
	@ResponseBody
	public LayuiTableVoJA searchGzFk(@ModelAttribute TsOrganizationInfo cond, String conditions){
		//查询条件
		String nameLinked = ConditionUtil.getConditionValue(conditions, "userName");//工号/姓名
		if (nameLinked != null){
			cond.where("B.UserId LIKE '%" + nameLinked + "%'" + " OR  B.UserName LIKE '%" + nameLinked + "%'");
		}
		cond.addField("A.OrgName  AS deptName");
		cond.addField("D.RoleName  AS roleName");
		cond.addField("(A.HeadUserId+ ',' +D.UserName)  AS userName");
		cond.addLeftJoin("TS_UserInfo D on A.HeadUserId = D.UserId  ");
		cond.setOrderby("A.createTime DESC");
		cond.setStatus(1);

		return commonApiService.searchLay(cond);
	}
}
